//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CotSwapFromFwdCorrelation.h"
using namespace Cephei::QL::Models::Marketmodels::Correlations;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/PiecewiseConstantCorrelation.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ fwdCorr, Cephei::QL::Models::Marketmodels::ICurveState^ curveState, Double displacement) : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
    CPiecewiseConstantCorrelation^ _CfwdCorr;
    CCurveState^ _CcurveState;
    try
    {
#ifdef HANDLE
        _phCotSwapFromFwdCorrelation = NULL;
#endif
        _CfwdCorr = safe_cast<CPiecewiseConstantCorrelation^> (fwdCorr);
        _CfwdCorr->Lock();
        boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>& _fwdCorr = static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>&> (_CfwdCorr->GetShared ()); 
        _CcurveState = safe_cast<CCurveState^> (curveState);
        _CcurveState->Lock();
        QuantLib::CurveState& _curveState = static_cast<QuantLib::CurveState&> (_CcurveState->GetReference ()); 
        QuantLib::Spread _displacement = (QuantLib::Spread)ValueHelper::Convert (displacement); //d
        _ppCotSwapFromFwdCorrelation = new boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation> (new QuantLib::CotSwapFromFwdCorrelation ( _fwdCorr,  _curveState,  _displacement ));
        SetPiecewiseConstantCorrelation (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfwdCorr != nullptr) _CfwdCorr->Unlock();
        if (_CcurveState != nullptr) _CcurveState->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation>& childNative, Object^ owner) : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
#ifdef HANDLE
	_phCotSwapFromFwdCorrelation = NULL;
#endif
	_ppCotSwapFromFwdCorrelation = &childNative;
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
}
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (QuantLib::CotSwapFromFwdCorrelation& childNative, Object^ owner) : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
#ifdef HANDLE
	_phCotSwapFromFwdCorrelation = NULL;
#endif
	_ppCotSwapFromFwdCorrelation = new boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation> (&childNative);
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
    _CotSwapFromFwdCorrelationOwner = owner;
    _PiecewiseConstantCorrelationOwner = owner;
}

Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (CCotSwapFromFwdCorrelation^ copy) : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
#ifdef HANDLE
	_phCotSwapFromFwdCorrelation = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCotSwapFromFwdCorrelation = new boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation> (copy->GetShared());
        _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (PLATFORM::Type^ t) : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
#ifdef HANDLE
	_phCotSwapFromFwdCorrelation = NULL;
#endif
	if (!t->IsSubclassOf(CCotSwapFromFwdCorrelation::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (QuantLib::Handle<QuantLib::CotSwapFromFwdCorrelation>& childNative, Object^ owner)  : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
	_phCotSwapFromFwdCorrelation = &childNative;
	_ppCotSwapFromFwdCorrelation = &static_cast<boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation>>(childNative.currentLink());
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
    _CotSwapFromFwdCorrelationOwner = owner;
}
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (QuantLib::Handle<QuantLib::CotSwapFromFwdCorrelation> childNative)  : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
	_phCotSwapFromFwdCorrelation = &childNative;
	_ppCotSwapFromFwdCorrelation = &static_cast<boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation>>(childNative.currentLink());
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::CCotSwapFromFwdCorrelation (QuantLib::CotSwapFromFwdCorrelation childNative)  : CPiecewiseConstantCorrelation(CCotSwapFromFwdCorrelation::typeid)
{
#ifdef HANDLE
	_phCotSwapFromFwdCorrelation = NULL;
#endif
	_ppCotSwapFromFwdCorrelation = new boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation> (new QuantLib::CotSwapFromFwdCorrelation (childNative));
    _ppPiecewiseConstantCorrelation = new boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation> (boost::dynamic_pointer_cast<QuantLib::PiecewiseConstantCorrelation> (*_ppCotSwapFromFwdCorrelation));
}
#endif

Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::~CCotSwapFromFwdCorrelation ()
{
    if (_ppCotSwapFromFwdCorrelation != NULL)
    {
	    delete _ppCotSwapFromFwdCorrelation;
        _ppCotSwapFromFwdCorrelation = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::!CCotSwapFromFwdCorrelation ()
{
    if (_ppCotSwapFromFwdCorrelation != NULL)
    {
	    delete _ppCotSwapFromFwdCorrelation;
    }
}
QuantLib::CotSwapFromFwdCorrelation& Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::GetReference ()
{
    if (_ppCotSwapFromFwdCorrelation == NULL) throw REFNEW NativeNullException ();
	return **_ppCotSwapFromFwdCorrelation;
}
boost::shared_ptr<QuantLib::CotSwapFromFwdCorrelation>& Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::GetShared ()
{
    if (_ppCotSwapFromFwdCorrelation == NULL) throw REFNEW NativeNullException ();
	return *_ppCotSwapFromFwdCorrelation;
}
QuantLib::CotSwapFromFwdCorrelation* Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::GetPointer ()
{
    if (_ppCotSwapFromFwdCorrelation == NULL) throw REFNEW NativeNullException ();
	return &**_ppCotSwapFromFwdCorrelation;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CotSwapFromFwdCorrelation>& Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::GetHandle ()
{
	if (_phCotSwapFromFwdCorrelation == NULL)
	{
		_phCotSwapFromFwdCorrelation = new Handle<QuantLib::CotSwapFromFwdCorrelation> (*_ppCotSwapFromFwdCorrelation);
	}
	return *_phCotSwapFromFwdCorrelation;
}
#endif
bool Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::HasNative () 
{
	return (_ppCotSwapFromFwdCorrelation != NULL);
}

Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::Correlations::get ()
{
    try
    {
    	std::vector<QuantLib::Matrix>& _rv = (std::vector<QuantLib::Matrix>&)(*_ppCotSwapFromFwdCorrelation)->correlations ( );   
        CoVector<Cephei::QL::Math::IMatrix^>^ _nrv = REFNEW CoVector<Cephei::QL::Math::IMatrix^>(REFNEW CMatrixVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppCotSwapFromFwdCorrelation)->numberOfRates ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::RateTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppCotSwapFromFwdCorrelation)->rateTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation::Times::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppCotSwapFromFwdCorrelation)->times ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Correlations::ICotSwapFromFwdCorrelation^ Cephei::QL::Models::Marketmodels::Correlations::CCotSwapFromFwdCorrelation_Factory::Create (Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ fwdCorr, Cephei::QL::Models::Marketmodels::ICurveState^ curveState, Double displacement)
{
    return REFNEW CCotSwapFromFwdCorrelation ( fwdCorr,  curveState,  displacement);
}
